<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Logic\Sms;
use App\Logic\Code;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Models\Admin;


class PanelController extends Controller
{

    /**
     * @OA\Post(
     *     path="/admin/panelIndex",
     *     tags={"后台仪表盘"},
     *     summary="数据",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function index(Request $request)
    {
        $time = time();
        $begin = strtotime(date('Y-m-d'));
        $no_begin_capacity_package = DB::table("miner_host as mh")
            ->join('miner_info as mi', 'mh.mid', '=', 'mi.mid')
            ->where('mh.start_time','>',$time)
            ->where('mi.note',1)
            ->sum('hspace');
        $no_begin_miner = DB::table("miner_host as mh")
            ->join('miner_info as mi', 'mh.mid', '=', 'mi.mid')
            ->where('mh.start_time','>',$time)
            ->where('mi.note',2)
            ->sum('hspace');

        $begin_capacity_package = DB::table("miner_host as mh")
            ->join('miner_info as mi', 'mh.mid', '=', 'mi.mid')
            ->where('mh.start_time','<',$time)
            ->where('mh.end_time','>',$time)
            ->where('mi.note',1)
            ->sum('hspace');
        $begin_miner = DB::table("miner_host as mh")
            ->join('miner_info as mi', 'mh.mid', '=', 'mi.mid')
            ->where('mh.start_time','<',$time)
            ->where('mh.end_time','>',$time)
            ->where('mi.note',2)
            ->sum('hspace');


        $already_withdraw = DB::table('withdraw')
            ->where('type','mb')
            ->where('state',1)
            ->where('token','FIL')
            ->sum('amount');

        $wait_withdraw = DB::table('miner_balance')
            ->where('token','FIL')
            ->sum('amount');


        $today_preformance = DB::table('order')
            ->where('status',1)
            ->where('check_time','>',$begin)
            ->where('check_time','<',$time)
            ->sum('all_cny');
        $all_preformance = DB::table('order')
            ->where('status',1)
            ->sum('all_cny');

        $arr = [
            'no_begin_capacity_package' => $no_begin_capacity_package,
            'no_begin_miner' => $no_begin_miner,
            'begin_capacity_package' => $begin_capacity_package,
            'begin_miner' => $begin_miner,
            'already_withdraw' => $already_withdraw,
            'wait_withdraw' => $wait_withdraw,
            'today_preformance' => $today_preformance,
            'all_preformance' => $all_preformance,
        ];
        return s_msg('',$arr);

    }




    /**
     * @OA\Post(
     *     path="/admin/panelList",
     *     tags={"后台仪表盘"},
     *     summary="列表",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *         @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"start_day","end_day"},
     *                 @OA\Property(property="start_day", type="string", description="格式 2020-02-18"),
     *                 @OA\Property(property="end_day", type="string", description="格式 2020-02-18"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function list(Request $request)
    {

        if($request->has('limit')){
            $limit = $request->input('limit');
        }else{
            $limit = 15;
        }
        $where = [];
        $post_data = $request->all();
        if(isset($post_data['start_day']) && $post_data['start_day'] != ""){
            $where[] = ['add_day','>=',$post_data['start_day']];
        }
        if(isset($post_data['end_day']) && $post_data['end_day'] != ""){
            $where[] = ['add_day','<=',$post_data['end_day']];
        }

        $list = DB::table('record_miner')
            ->where($where)
            ->orderByDesc('add_day')
            ->paginate($limit);

        return s_msg('',$list);

    }


    /**
     * @OA\Post(
     *     path="/admin/panel_list_xq",
     *     tags={"后台仪表盘"},
     *     summary="列表详情",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"add_day"},
     *                 @OA\Property(property="add_day", type="string", description="格式"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常操作响应",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                  @OA\Property(property="data", type="string",description="123")
     *             )
     *         )
     *     )
     * )
     */
    public function list_xq(Request $request)
    {

        if($request->has('add_day')){
            $add_day = $request->input('add_day');

            $info = DB::table('record_miner')
                ->where('add_day',$add_day)
                ->first();
            return s_msg('',$info);
        }else{
            return e_msg(300015,'请传入时间 add_day');
        }

    }


}
